# -*- coding: utf-8 -*-

"""
  @author: 唐黎琦
  @date: 2022/6/18 11:13
  @python version: 3.6 
  
  ---------------------------------------
  @desc: PyCharm => python设计模式实现
"""
from abc import ABC, abstractmethod


# 通过一个手机、电脑的例子来实现工厂模式的讲解

# ============================  实现一个简单工厂  ============================
# 生产苹果和华为手机的简单工厂

class Phone(ABC):
    @abstractmethod
    def power_on(self):
        pass

    @abstractmethod
    def power_off(self):
        pass


class Iphone(Phone):
    def power_on(self):
        pass

    def power_off(self):
        pass


class HuaweiPhone(Phone):
    def power_on(self):
        pass

    def power_off(self):
        pass


class PhoneFactory:

    @staticmethod
    def create(phone_type):
        if phone_type == "iphone":
            return Iphone
        if phone_type == "huawei":
            return HuaweiPhone


huawei_phone = PhoneFactory().create("huawei")
iphone = PhoneFactory().create("iphone")


# ============================  实现一个工厂方法  ============================
# 生产华为和戴尔电脑的工厂方法
class Computer(ABC):
    @abstractmethod
    def power_on(self):
        pass


class HuaweiComputer(Computer):
    def power_on(self):
        pass


class DellComputer(Computer):
    def power_on(self):
        pass


class ComputerFactory(ABC):
    @abstractmethod
    def create(self):
        pass


class HuaweiComputerFactory(ComputerFactory):
    def create(self):
        return HuaweiComputer


class DellComputerFactory(ComputerFactory):
    def create(self):
        return DellComputer


huawei_pc = HuaweiComputerFactory().create()
dell_pc = DellComputerFactory().create()


# ============================  实现一个抽象工厂  ============================
class Desktop(ABC):
    @abstractmethod
    def power_on(self):
        pass

    @abstractmethod
    def power_off(self):
        pass


class HuaweiDesktop(Desktop):
    def power_off(self):
        pass

    def power_on(self):
        pass


class MacBook(Desktop):
    def power_on(self):
        pass

    def power_off(self):
        pass


# 抽象工厂类
class BrandFactory(ABC):
    @abstractmethod
    def create_phone(self):
        pass

    @abstractmethod
    def create_desktop(self):
        pass


# 抽象工厂实现子类
class HuaweiFactory(BrandFactory):
    def create_desktop(self):
        return HuaweiDesktop

    def create_phone(self):
        return HuaweiPhone


# 抽象工厂实现子类
class AppleFactory(BrandFactory):
    def create_phone(self):
        return Iphone

    def create_desktop(self):
        return MacBook


# 具体的产品由工厂中具体的方法实现
huawei_factory = HuaweiFactory()
huawei_matebook = huawei_factory.create_desktop()
huawei_p50 = huawei_factory.create_phone()

apple_factory = AppleFactory()
iphone13 = apple_factory.create_phone()
macbook_pro = apple_factory.create_desktop()
